<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/Smart_Backstage/layui/css/layui.css">
    <script src="/Smart_Backstage/layui/layui.js" charset="utf-8"></script>
    <script src="\Smart_Backstage\js\jquery-3.4.1.js"></script>

    <style type="text/css">

        .layui-body{
            background-color: #F0F0F0;
        }

        #doContainer{
            background-color: white;
            border-top: 2px solid #1E9FFF;
            margin: 20px;
            border-radius:10px;
        }

        #tableDiv{
            padding: 25px;
        }


        #daohTitle{
            padding-top: 25px;
            padding-left: 35px;
            padding-bottom: 15px;
            display: inline-block;
        }

    </style>

</head>

<body class="layui-layout-body">

<div class="layui-layout layui-layout-admin">

    <div class="header"></div>

    <div class="left"></div>

    <div class="layui-body">
        <!-- 内容主体区域-->
        <div id="doContainer">
                <span class="layui-breadcrumb" id="daohTitle">
                    <a href="index.html">首页</a>
                    <a href="#">建议系统</a>
                    <a><cite>用户建议</cite></a>
                </span>

            <div id="tableDiv">

                <div class="layui-btn-group demoTable">
                    <button class="layui-btn" data-type="getCheckLength">删除</button>
                    <button class="layui-btn" data-type="isAll">刷新</button>
                </div>

                <table class="layui-table" lay-filter="demo" id="demo" ></table>
            </div>
        </div>
    </div>

    <div class="footer"></div>

</div>



<script id="barDemo" type="text/html">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script>

    layui.use(['table','laydate','layer'], function(){
        var table = layui.table,
            $ = layui.jquery,
            layer = layui.layer,
            laydate = layui.laydate,
            form = layui.form;
        //监听表格复选框选择
        table.on('checkbox(demo)', function(obj){
            console.log(obj)
        });

        //触发事件
        var active = {
            getCheckLength: function(){ //选中删除
                var checkStatus = table.checkStatus('demo')
                    ,data = checkStatus.data;
                if(data.length==0){
                    layer.msg("没有选中内容");
                }else{
                    var arrID = [];
                    for(var i = 0;i<data.length;i++){
                        arrID.push(data[i].id)
                    }
                    $.ajax({
                        // 编写json格式，设置属性和值
                        url: "/Smart_Backstage/sugest/del",
                        contentType: "application/json;charset=UTF-8",
                        data: JSON.stringify(arrID),
                        dataType: "json",
                        type: "post",
                        success: function (data) {
                            if(data == 1){
                                render();
                            }
                        }
                    })
                }
            }
            ,isAll: function(){ //刷新
                render();
            }
        };

        //数据的查看，编辑，删除
        table.on('tool(demo)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){//查看详情
                //layer.msg('ID：'+ data.id + ' 的查看操作');
                dialog2(data.id);
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    var arrId = [];
                    arrId.push(data.id);
                    $.ajax({
                        // 编写json格式，设置属性和值
                        url: "/Smart_Backstage/sugest/del",
                        contentType: "application/json;charset=UTF-8",
                        data: JSON.stringify(arrId),
                        dataType: "json",
                        type: "post",
                        success: function (data) {
                            obj.del();
                            layer.close(index);
                        }
                    })

                });
            }
        });


        //时间初始化
        function showTime(time){
            var date = new Date(time);
            var year = date.getFullYear();
            var month = date.getMonth();
            var day = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (day >= 0 && day <= 9) {
                day = "0" + day;
            }
            return year+'-'+month+'-'+day;
        }



        render();

        function render(){
            table.render({
                elem: '#demo'
                ,url: '/Smart_Backstage/sugest/findAll'
                ,page:true
                ,cols: [[ //标题栏
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'id', title: 'ID', width: 80, sort: true}
                    ,{field: 'name', title: '姓名', width: 100,templet:'<div>{{d.user.name}}</div>'}
                    ,{field: 'title', title: '标题', width: 150}
                    ,{field: 'message', title: '建议内容', width: 661}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:200}
                ]]
                ,parseData: function(pageInfo){ //res 即为原始返回的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": '内容为空', //解析提示文本
                        "count": pageInfo.total, //解析数据长度
                        "data": pageInfo.list //解析数据列表
                    };
                }
            });
        }


        //查看的dialog
        function dialog2(id_) {
            $.ajax({
                // 编写json格式，设置属性和值
                url: "/Smart_Backstage/sugest/findById",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(id_),
                dataType: "json",
                type: "post",
                success: function (data) {
                    if (data!=undefined) {
                        layer.open({
                            type: 1
                            , title: false //不显示标题栏
                            , closeBtn: false
                            , area: '700px;'
                            , shade: 0.8
                            //,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                            , btn: ['确定']
                            , btnAlign: 'c'
                            , content:
                                '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
                                '    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;margin-bottom: 20px; margin-bottom: 40px;" >\n' +
                                '        <legend>建议</legend>\n' +
                                '    </fieldset>' +
                                '<ul class="layui-timeline">\n' +
                                '  <li class="layui-timeline-item">\n' +
                                '    <i class="layui-icon layui-timeline-axis"></i>\n' +
                                '    <div class="layui-timeline-content layui-text" style="color: #F0F0F0">\n' +
                                '      <h3 class="layui-timeline-title" style="color: white;">' + data.user.name + '&nbsp;&nbsp;先生/女士的建议</h3>\n' +
                                '      <p>\n' +
                                '        标题：' + data.title +
                                '        <br>内容：' + data.message +
                                '      </p>\n' +
                                '    </div>\n' +
                                '  </li>' +
                                '</div>'

                        });
                    }
                }

            });
        }


        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>

<script>

    $(function(){
        $('.header').load('header.html');
        $('.left').load('aside.html');
        $('.footer').load('footer.html');
    })

    //JavaScript代码区域
    $("#content") .load("intoIndex",function () {

        layui.use('element', function(){
            var element = layui.element;
        });

    });
</script>


</body>
</html>